$(document).ready(function() {
    $('#datepicker').datepicker();//调用日历控件

    formValidate();
    autoComplete();
    autoComplete1();
    continueAdd();
    $('.icon-calendar').bind('click',function(e){
        $(this).parent().prev().trigger('click');
        e.stopPropagation();
    });
    $("input[name='parent_input[]'],#input_type").bind('change',function(){
        if(!$.trim($(this).val())){
            $(this).parent().children("input").val("");
        }
    });
});
function formValidate(){
    //表单验证
    $('#myForm').validate({
        onsubmit: true,
        submitHandler: function(form){
            form.submit();
        },
        errorPlacement: function(error, element) {
            if ( element.is(":radio") )
                error.appendTo( element.parent().next().next() );
            else if ( element.is(":checkbox") )
                error.appendTo ( element.next() );
            else
                element.parent().append(error);
        }

    });
    jQuery.extend(jQuery.validator.messages, {
        required: "请填写本字段",
        date: "请输入正确的日期",
        min: "请选择",
        digits: "请输入正确的整数"
    });
}
//搜索职位
function autoComplete(){
    $('#input_type').autocomplete({
        source: function(request, response) {
            $.ajax({
                url: ajaxUrl2,
                dataType: "json",
                data: {
                    name: request.term
                },
                success: function(data) {
                    response( $.map( data.data, function( item ) {
                        return {
                            label: item.job_name,
                            value: item.job_name,
                            id: item.job_id,
                            city_id:item.city_id,
                            city:item.city_name
                        }
                    }));
                }
            });
        },
        select: function( event, ui ) {console.log('ui.item:',ui.item);
            //ui.item && $(this).parent().find('input[type="hidden"]').val(ui.item.job_id);
            var id = ui.item.id,
                cityID = ui.item.city_id,
                city = ui.item.city;

            var input1 = $(this).parent().find('input[type="hidden"]')[0],
                input2 = $(this).parent().find('input[type="hidden"]')[1],
                work_city = $('input[name="work_city"]');
            isSelected = true;
            input1.value = id;
            input2.value = cityID;
            work_city.val(city);
        }
    })
}
//搜索员工
function autoComplete1(){
    $('.search_name').autocomplete({
        source: function(request, response) {
            $.ajax({
                url: ajaxUrl,
                dataType: "json",
                data: {
                    name: request.term
                },
                success: function(data) {
                    response( $.map( data.data, function( item ) {
                       var temp =  {
                            label: item.name,
                            value: item.name,
                            id: item.user_id,
                            job_id:item.job_id
                        };

                        return temp;
                    }));
                }
            });
        },
        select: function( event, ui ) {console.log('item:',ui.item);
            //ui.item && $(this).parent().find('input[type="hidden"]').val(ui.item.id);

            var id = ui.item.id,
                job_id = ui.item.job_id;

            var input1 = $(this).parent().find('input[type="hidden"]')[0];
            var input2 = $(this).parent().find('input[type="hidden"]')[1];

            input1.value = id;
            input2.value = job_id;
        }
    })
}

//添加虚线主管
function continueAdd(){
    $('.continueAdd').bind('click',function(){
        //$(".panel_conti:first").after($(".panel_conti:first").clone());
        var i=1;
        var lastElm = $('.panel_conti').last();

        lastElm.after('<div class="panel_conti">' + lastElm.html().replace(/\[(\d)\]/g,function () {
            return '[]';
        }) + '</div>');

        i++;

        var nextElm = lastElm.next().find('.search_name');
        nextElm.val('');

        lastElm.find('.continueAdd').remove();

        continueAdd();

        //搜索员工
        autoComplete1();
    });
}
